package com.party.util.easyexcel.style;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import org.apache.poi.ss.usermodel.*;

/**
 * 通用excel导出样式
 *
 * @author gcx
 * @date 2019/12/20
 */
public class GeneralCellStyleStrategy extends AbstractCellStyleStrategy {
    private CellStyle headCellStyle;
    private CellStyle contentCellStyle;
    private CellStyle richTextCellStyle;

    @Override
    protected void initCellStyle(Workbook workbook) {
        initHeadCellStyle(workbook);
        initContentCellStyle(workbook);
        initRichTextCellStyle(workbook);
    }

    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        cell.setCellStyle(headCellStyle);
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        cell.setCellStyle(cell.getCellTypeEnum() == CellType.STRING ? richTextCellStyle : contentCellStyle);
    }

    private void initHeadCellStyle(Workbook workbook) {
        WriteCellStyle titleStyle = new WriteCellStyle();
        titleStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
        titleStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        titleStyle.setBorderBottom(BorderStyle.THIN);
        titleStyle.setBorderLeft(BorderStyle.THIN);
        titleStyle.setBorderRight(BorderStyle.THIN);
        titleStyle.setBorderTop(BorderStyle.THIN);
        titleStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        WriteFont titleFont = new WriteFont();
        titleFont.setColor(IndexedColors.VIOLET.getIndex());
        titleFont.setFontHeightInPoints(Short.valueOf("12"));
        titleFont.setBold(true);
        titleStyle.setWriteFont(titleFont);
        headCellStyle = StyleUtil.buildHeadCellStyle(workbook, titleStyle);
    }

    private void initContentCellStyle(Workbook workbook) {
        WriteCellStyle cellStyle = new WriteCellStyle();
        cellStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        cellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        WriteFont cellFont = new WriteFont();
        cellFont.setFontHeightInPoints(Short.valueOf("11"));
        cellFont.setBold(false);
        cellStyle.setWriteFont(cellFont);
        contentCellStyle = StyleUtil.buildHeadCellStyle(workbook, cellStyle);
    }

    private void initRichTextCellStyle(Workbook workbook) {
        WriteCellStyle cellStyle = new WriteCellStyle();
        cellStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        cellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        WriteFont cellFont = new WriteFont();
        cellFont.setFontHeightInPoints(Short.valueOf("11"));
        cellFont.setBold(false);
        cellFont.setColor(IndexedColors.BLUE.getIndex());
        cellStyle.setWriteFont(cellFont);
        richTextCellStyle = StyleUtil.buildHeadCellStyle(workbook, cellStyle);
    }


}
